package com.example.bestapp;

import android.os.Bundle;
import android.util.JsonToken;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;


import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.bestapp.adapter.RecycleViewDemoAdapter;
import com.example.bestapp.bean.UserBean;
import com.example.bestapp.utils.LogUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;


public class RecycleViewDemo extends AppCompatActivity {
    private ArrayList<UserBean> datas;
    private RecycleViewDemoAdapter adapter;
    RecyclerView recyclerView;
    private String json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_view_demo);

        Toolbar toolbar =findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initdata();
        initview();
        UserBean.isShow=false;
    }

    private void initview() {
        recyclerView=findViewById(R.id.recycleview);
        TextView edit= findViewById(R.id.edit);
        View footer=findViewById(R.id.footer);
        TextView tv_delete=findViewById(R.id.tv_delete);
        TextView tv_select_all=findViewById(R.id.tv_select_all);

        LinearLayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        adapter=new RecycleViewDemoAdapter(this,datas);
        recyclerView.setAdapter(adapter);

        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserBean.isShow=!UserBean.isShow;
                int show= footer.getVisibility()==View.GONE?View.VISIBLE:View.GONE;
                footer.setVisibility(show);
                if (UserBean.isShow){
                    edit.setText("取消");
                } else {
                    edit.setText("编辑");
                }
                adapter.notifyDataSetChanged();
            }
        });
//        ------------------------------------------------------------------------------

        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = datas.size()-1; i >=0 ; i--) {
                        if (datas.get(i).getSelect()){
                            datas.remove(i);
                            adapter=new RecycleViewDemoAdapter(RecycleViewDemo.this,datas);
                            recyclerView.setAdapter(adapter);
                    }
                }
            }
        });
        tv_select_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (UserBean userBean:datas){
                    userBean.setSelect(!userBean.getSelect());
                }
                adapter.notifyDataSetChanged();
            }
        });
    }



    private void initdata() {
        datas = new ArrayList<>();
        datas.addAll(UserBean.loadData(RecycleViewDemo.this));
    }



    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();

        }
        return super.onOptionsItemSelected(item);
    }

}